package 并发编程.this引用逃逸;

public class ThisEscapeSolver {
    private Thread thread;
    private int num = 0;

    public ThisEscapeSolver() throws InterruptedException {
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                doSomething();
            }
        });
        Thread.sleep(1000);
        num = 10;
    }

    public void init() {
        thread.start();
    }

    public static ThisEscapeSolver newInstance() throws InterruptedException {
        ThisEscapeSolver thisEscapeSolver = new ThisEscapeSolver();
        thisEscapeSolver.init();
        return thisEscapeSolver;
    }

    public void doSomething() {
        System.out.println("num = " + num);
    }

    public static void main(String args[]) throws InterruptedException {
        ThisEscapeSolver.newInstance();
    }
}
